package crossbreed.utilities;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class SimpleFileReader implements ISimpleFileReader
{
	private ArrayList<String> lines;
	private String filePath;
	
	@Override
	public String getPath()
	{
		return filePath;
	}
	
	@Override
	public void setPath(String filePath)
	{
		this.filePath = filePath;
	}

	@Override
	public ArrayList<String> readAllLines() throws IOException
	{
		if (lines != null) return lines;
		
		FileReader fileReader = new FileReader(filePath);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		String currentLine = bufferedReader.readLine();
		lines = new ArrayList<String>();

		while (currentLine != null)
		{
			lines.add(currentLine);
			currentLine = bufferedReader.readLine();
		}

		return lines;
	}
	
	@Override
	public String readAsString() throws IOException
	{
		StringBuilder builder = new StringBuilder();
		ArrayList<String> lines = readAllLines();
		for (String line : lines) builder.append(line).append("\r\n");
		return builder.toString();
	}
}
